﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ChildrenLoadedHandler = TreeBuilder.Contracts.ChildrenLoadedHandler;
using AbstraX.ClientInterfaces;
using TreeBuilder.Contracts;
using ServerEntities = SolutionFramework.Entities;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using UIObjects;
using System.Diagnostics;

namespace SolutionFramework.Entities.TreeBuilder.Client
{
    public class TreeNodeRootSource : ITreeNodeRootSource
    {
        public event ChildrenLoadedHandler ChildrenLoaded;
        private ServerEntities.TreeNodeRootSource treeNodeRootSource;
        private ITreeBuilderService service;
        private ObservableCollection<ITreeNodeBase> children;
        private IBase source;
        public ITreeNodeBase ReferenceNode { get; set; }
        public ITreeNodeBase ShortcutNode { get; set; }
        public event NodeOperationSuccessful RemovedSuccessfully;
        public event NodeOperationSuccessful UpdatedSuccessfully;
        public ILog Log { get; set; }

        public TreeNodeRootSource(ITreeBuilderService service, ServerEntities.TreeNodeRootSource treeNodeRootSource)
        {
            this.treeNodeRootSource = treeNodeRootSource;
            this.service = service;

            var itemGenerationOp = service.GenerateItemFromID<IBase>(treeNodeRootSource.Source);

            itemGenerationOp.ExceptionOccured += (sender, e) =>
            {
                Debugger.Break();
            };

            itemGenerationOp.Generated += (sender, e) =>
            {
                this.source = e.Item;
            };
        }

        public IBase BaseObject
        {
            get
            {
                return source;
            }
            set
            {
                source = value;
            }
        }

        public Entity ServerEntity
        {
            get 
            {
                return treeNodeRootSource;
            }
        }

        public IBase Source
        {
            get
            {
                return source;
            }
            set
            {
                source = value;
            }
        }

        public string ID
        {
            get
            {
                return treeNodeRootSource.TreeNodeBaseID.ToString();
            }
        }

        public string Text
        {
            get
            {
                return treeNodeRootSource.TreeNodeBase.Name;
            }

            set
            {
                treeNodeRootSource.TreeNodeBase.Name = value;
                service.UpdateNode<ITreeNodeRootSource>(this);
            }
        }

        public void LoadChildren()
        {
            children = service.LoadChildren(this);

            ((INotifyCollectionChanged)children).CollectionChanged += new NotifyCollectionChangedEventHandler(ChildrenCollectionChanged);
        }

        private void ChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ChildrenLoaded(this, EventArgs.Empty);
        }

        public void NoChildren()
        {
            ChildrenLoaded(this, EventArgs.Empty);
        }

        public System.Collections.Generic.IEnumerable<ITreeNodeBase> Children
        {
            get
            {
                return children;
            }
        }

        public string ParentID
        {
            get
            {
                return treeNodeRootSource.TreeNodeBase.ParentNodeID.ToString();
            }
        }

        public void Remove()
        {
            var submitOperation = service.DeleteNode<ITreeNodeRootSource>(this);

            submitOperation.Completed += (sender, e) =>
            {
                RemovedSuccessfully(this, e);
            };
        }

        public ITreeNodeBase ShallowClone()
        {
            throw new NotImplementedException();
        }

        public ITreeNodeBase DeepClone()
        {
            throw new NotImplementedException();
        }

        ITreeNodeServerEntity IClonable<ITreeNodeServerEntity>.ShallowClone()
        {
            throw new NotImplementedException();
        }

        ITreeNodeServerEntity IClonable<ITreeNodeServerEntity>.DeepClone()
        {
            throw new NotImplementedException();
        }

        public ITreeNodeBase InternalNodeObject
        {
            get { throw new NotImplementedException(); }
        }

        public float ChildOrdinal
        {
            get
            {
                var childOrdinal = treeNodeRootSource.TreeNodeBase.ChildOrdinal ?? (float?)0.0f;

                return (float)childOrdinal;
            }
            set
            {
                treeNodeRootSource.TreeNodeBase.ChildOrdinal = value;
                service.UpdateNode<TreeNodeRootSource>(this);
            }
        }

        public bool HasLayoverImage<T>(out T image)
        {
            image = default(T);
            return false;
        }
    }
}
